package OrdenarNPalabras;

import anaydis.sort.Sorter;
import anaydis.sort.gui.ObservableSorter;
import benchMark.BenchMarkImpl;
import data.IntegerDataSetGenerator;
import provider.SorterProviderImpl;
import java.io.*;
import java.util.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Keevu
 * Date: 05/10/11
 * Time: 15:43
 * To change this template use File | Settings | File Templates.
 */
public class OrdenarNPalabras {

    public static void main(String[] args) throws IOException {
        IntegerDataSetGenerator dataGenerator = new IntegerDataSetGenerator();
        SorterProviderImpl sorterProvider = new SorterProviderImpl();
        Iterable<Sorter> sorters = sorterProvider.getAllSorters();
        algorithmTest(dataGenerator, sorters);
    }

    private static void algorithmTest(IntegerDataSetGenerator dataGenerator, Iterable<Sorter> sorters) throws IOException {
        System.out.println("Resultado de las pruebas:");
        System.out.println("");
        try {
            testing(dataGenerator, sorters);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private static void testing(IntegerDataSetGenerator dataGenerator, Iterable<Sorter> sorters) throws Exception {
        for (int n = 10; n < 2000; n = n * 5) {
            System.out.println("primeras "+n+" palabras:");
            for (Sorter sorter : sorters) {
                List list=new ArrayList();
                File f = new File( "src\\OrdenarNPalabras\\moby_dick.txt" );
                BufferedReader entrada = new BufferedReader( new FileReader(f));
                   for(int i=0;i<n;i++){
                    if ( f.exists() ) {
                        list.add((entrada.read()));
                    }
                   }

                BenchMarkImpl benchMark = new BenchMarkImpl();
                ((ObservableSorter) sorter).addSorterListener(benchMark);
                long firstTime = System.currentTimeMillis();
                sorter.sort(dataGenerator.getComparator(), list);
                long lastTime = System.currentTimeMillis();
                System.out.println("Con el metodo "+ sorter.getType().toString()+ " Se registraron los datos: Tiempo: " + (lastTime - firstTime) + " MiliSeg, " +
                        +benchMark.getSwaps() + " Swaps ," + benchMark.getComparisons() + " Comparaciones");
            }
            System.out.println("");
        }
    }
}
